package wangm.netty;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.Scanner;

/**
 * describe
 *
 * @author zk
 * @date 2018-02-03 14:58
 */
@ChannelHandler.Sharable
public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf>{
    String message = "public static void main(String[] args) throws Exception {\n" +
            "    // Configure the server.\n" +
            "    EventLoopGroup bossGroup = new NioEventLoopGroup();\n" +
            "    EventLoopGroup workerGroup = new NioEventLoopGroup();\n" +
            "    try {\n" +
            "        ServerBootstrap b = new ServerBootstrap();\n" +
            "        b.group(bossGroup, workerGroup)\n" +
            "         .channel(NioServerSocketChannel.class)\n" +
            "         .option(ChannelOption.SO_BACKLOG, 100)\n" +
            "         .localAddressLoopGroup bossGroup = new NioEventLoopGroup();\n" +
            "    EventLoopGroup workerGroup = new NioEventLoopGroup();\n" +
            "    try {\n" +
            "        ServerBootstrap b = new ServerBootstrap();\n" +
            "        b.group(bossGroup, workerGroup)\n" +
            "         .channel(NioServerSocketChannel.class)\n" +
            "         .option(ChannelOption.SO_BACKLOG, 100)\n" +
            "         .localAddressLoopGroup bossGroup = new NioEventLoopGroup();\n" +
            "    EventLoopGroup workerGroup = new NioEventLoopGroup();\n" +
            "    try {\n" +
            "        ServerBootstrap b = new ServerBootstrap();\n" +
            "        b.group(bossGroup, workerGroup)\n" +
            "         .channel(NioServerSocketChannel.class)\n" +
            "         .option(ChannelOption.SO_BACKLOG, 100)\n" +
            "         .localAddressLoopGroup bossGroup = new NioEventLoopGroup();\n" +
            "    EventLoopGroup workerGroup = new NioEventLoopGroup();\n" +
            "    try {\n" +
            "        ServerBootstrap b = new ServerBootstrap();\n" +
            "        b.group(bossGroup, workerGroup)\n" +
            "         .channel(NioServerSocketChannel.class)\n" +
            "         .option(ChannelOption.SO_BACKLOG, 100)\n" +
            "         .localAddressLoopGroup bossGroup = new NioEventLoopGroup();\n" +
            "    EventLoopGroup workerGroup = new NioEventLoopGroup();\n" +
            "    try {\n" +
            "        ServerBootstrap b = new ServerBootstrap();\n" +
            "        b.group(bossGroup, workerGroup)\n" +
            "         .channel(NioServerSocketChannel.class)\n" +
            "         .option(ChannelOption.SO_BACKLOG, 100)\n" +
            "         .localAddress(8080)\n" +
            "         .childOption(ChannelOption.TCP_NODELAY, true)\n" +
            "         .childHandler(new ChannelInitializer<SocketChannel>() {\n" +
            "             @Override\n" +
            "             public void initChannel(SocketChannel ch) throws Exception {\n" +
            "                 ch.pipeline().addLast(handler1, handler2, ...);\n" +
            "             }\n" +
            "         });\n" +
            "\n" +
            "        // Start the server.\n" +
            "        ChannelFuture f = b.bind().sync();\n" +
            "\n" +
            "        // Wait until the server socket is closed.\n" +
            "        f.channel().closeFuture().sync();\n" +
            "    } finally {\n" +
            "        // Shut down all event loops to terminate all threads.\n" +
            "        bossGroup.shutdownGracefully();\n" +
            "        workerGroup.shutdownGracefully();\n" +
            "\n" +
            "        // Wait until all threads are terminated.\n" +
            "        bossGroup.terminationFuture().sync();\n" +
            "        workerGroup.terminationFuture().sync();\n" +
            "    }\n" +
            "}What is the difference between ServerBootStrap.option and ServerBootStrap.childOption?\n" +
            "\n\n" +
            "        // Start the server.\n" +
            "        ChannelFuture f = b.bind().sync();\n" +
            "\n" +
            "        // Wait until the server socket is closed.\n" +
            "        f.channel().closeFuture().sync();\n" +
            "    } finally {\n" +
            "        // Shut down all event loops to terminate all threads.\n" +
            "        bossGroup.shutdownGracefully();\n" +
            "        workerGroup.shutdownGracefully();\n" +
            "\n" +
            "        // Wait until all threads are terminated.\n" +
            "        bossGroup.terminationFuture().sync();\n" +
            "        workerGroup.terminationFuture().sync();\n" +
            "    }\n" +
            "}";
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        while(true){
            ByteBuffer sendBuf = ByteBuffer.allocate(4*4);
            sendBuf.putInt(12);
            ctx.writeAndFlush(sendBuf);
        }

//        ctx.writeAndFlush(Unpooled.copiedBuffer(message,CharsetUtil.UTF_8));
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) throws Exception {
//        System.out.println("client read:" + msg.toString(CharsetUtil.UTF_8));
    }


}
